<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    console.log('PieChart组件挂载完成')
    this.$nextTick(() => {
      console.log('PieChart $nextTick执行')
      this.initChart()
      // 监听数据更新事件
      this.$on('update-chart-data', this.updateData)
      console.log('PieChart事件监听器已设置')
      console.log('PieChart DOM元素:', this.$el)
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      console.log('开始初始化PieChart，DOM元素:', this.$el)
      if (!this.$el) {
        console.error('PieChart DOM元素不存在')
        return
      }

      this.chart = echarts.init(this.$el, 'macarons')
      console.log('PieChart已初始化:', this.chart)

      // 初始化图表，等待父组件更新数据
      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{b}: {c} ({d}%)'
        },
        legend: {
          orient: 'vertical',
          left: 'left',
          top: '20%',
          textStyle: {
            fontSize: 12
          }
        },
        series: [
          {
            name: '科室预约占比',
            type: 'pie',
            radius: ['40%', '70%'],
            center: ['60%', '50%'],
            avoidLabelOverlap: false,
            label: {
              show: true,
              fontSize: 12,
              formatter: '{b}: {d}%'
            },
            emphasis: {
              label: {
                show: true,
                fontSize: 14,
                fontWeight: 'bold'
              }
            },
            labelLine: {
              show: true
            },
            data: [], // 空数据，等待父组件更新
            animationEasing: 'cubicInOut',
            animationDuration: 2000
          }
        ],
        color: ['#409EFF', '#67C23A', '#E6A23C', '#F56C6C', '#909399', '#795AEE', '#00C4CC', '#FF8C42']
      })

      // 保存图表实例供父组件使用
      if (!window.pieChartInstance) {
        window.pieChartInstance = this.chart
      }
      console.log('PieChart实例已创建并保存到window对象:', this.chart)
    },
    updateData(data) {
      console.log('PieChart接收到数据更新请求')
      console.log('接收到的数据:', data)
      console.log('当前图表实例:', this.chart)

      if (this.chart && data) {
        const chartData = data.length > 0 ? data : [
          { name: '暂无数据', value: 1 }
        ]

        console.log('设置图表数据:', chartData)

        this.chart.setOption({
          tooltip: {
            trigger: 'item',
            formatter: '{b}: {c} ({d}%)'
          },
          legend: {
            orient: 'vertical',
            left: 'left',
            top: '20%',
            textStyle: {
              fontSize: 12
            }
          },
          series: [
            {
              name: '科室预约占比',
              type: 'pie',
              radius: ['40%', '70%'],
              center: ['60%', '50%'],
              avoidLabelOverlap: false,
              label: {
                show: true,
                fontSize: 12,
                formatter: '{b}: {d}%'
              },
              emphasis: {
                label: {
                  show: true,
                  fontSize: 14,
                  fontWeight: 'bold'
                }
              },
              labelLine: {
                show: true
              },
              data: chartData,
              animationEasing: 'cubicInOut',
              animationDuration: 2000
            }
          ],
          color: ['#409EFF', '#67C23A', '#E6A23C', '#F56C6C', '#909399', '#795AEE', '#00C4CC', '#FF8C42']
        }, true)

        console.log('图表数据更新完成')
      } else {
        console.warn('图表实例或数据不存在:', { chart: this.chart, data: data })
      }
    }
  }
}
</script>
